#!/bin/bash
# Author: Redbrother
# Email: 728083668@qq.com
# WebSite: https://help.ubuntu.com/community/LiveCDCuyystomizationFromScratch
# license: None
# data :2019 10
# scripts in /usr/share/debootstrap/scripts

############################################

ARCH=amd64
RELEASE=bionic
installer_url=http://mirrors.aliyun.com/ubuntu/
IMAGE=ubuntu-$RELEASE

set -e
#自定义版本项

if [ "`whoami`" != "root" ]; then
	echo "User must be root 需要root权限."
	exit 0
fi
apt install debootstrap genisoimage p7zip-full squashfs-tools -y
if [ "$?" != "0" ]; then
	echo "Commands required: mkisofs, 7z, wget."
	exit 0
fi
if [ "`which mkisofs`" == "" ]; then
	ln -s /usr/bin/genisoimage /usr/bin/mkisofs
fi

############################################

# debootstrap a base live
umount squashfs-root/* 2>/dev/null || true
rm -rf squashfs-root
mkdir -p squashfs-root
debootstrap --arch=$ARCH $RELEASE squashfs-root $installer_url

############################################

cd squashfs-root
echo "deb $installer_url $RELEASE main restricted universe multiverse"          >  etc/apt/sources.list
echo "deb $installer_url $RELEASE-updates main restricted universe multiverse"  >> etc/apt/sources.list
echo "deb $installer_url $RELEASE-security main restricted universe multiverse" >> etc/apt/sources.list
echo 'APT::Install-Recommends "false";' > etc/apt/apt.conf
chroot . chmod a+s /bin/ping
cp -f /etc/hosts etc/hosts
rm -f etc/resolv.conf
mkdir -p run/NetworkManager/
cp -f /etc/resolv.conf etc/
mount -o bind /dev dev/
mount -o bind /sys sys/
mount -o bind /proc proc/
mount -o bind /dev/pts dev/pts/
chroot . rm -f /sbin/initctl
chroot . dbus-uuidgen > /var/lib/dbus/machine-id
chroot . dpkg-divert --local --rename --add /sbin/initctl
chroot . ln -s /bin/true /sbin/initctl
chroot . apt-get update
chroot . apt-get upgrade --yes
chroot . locale-gen zh_CN.UTF-8
chroot . locale-gen en_US.UTF-8
# kernels and network
echo -e "install kernels and network …… "   
DEBIAN_FRONTEND=noninteractive chroot . apt-get install --no-install-recommends \
     dbus sudo bash-completion grub-pc linux-image-generic xinit xserver-xorg network-manager apt-transport-https --yes
DEBIAN_FRONTEND=noninteractive chroot . apt-get download network-manager --yes
# Livecd necessary 
echo -e "install Livecd necessary  "  
DEBIAN_FRONTEND=noninteractive chroot . apt-get install --no-install-recommends \
     ubuntu-standard casper lupin-casper discover laptop-detect os-prober --yes
# useful tools
echo -e "install useful tools  "  
DEBIAN_FRONTEND=noninteractive chroot . apt-get install --no-install-recommends \
     vim nano gvfs lzma unrar rar unzip zip genisoimage p7zip-full wget aria2 zsync jigdo-file \
     squashfs-tools debootstrap syslinux rsync dump parted gparted mdadm python3 python3-distutils --yes
# auto login
mkdir -p etc/lightdm/lightdm.conf.d
mkdir -p usr/share/lightdm/lightdm.conf.d
echo '. /etc/bash_completion'   >> etc/bash.bashrc
echo '[SeatDefaults]'           >  etc/lightdm/lightdm.conf.d/90-default.conf
echo 'autologin-user=ubuntu'    >> etc/lightdm/lightdm.conf.d/90-default.conf
echo 'autologin-user-timeout=0' >> etc/lightdm/lightdm.conf.d/90-default.conf
echo 'autologin-guest=false'    >> etc/lightdm/lightdm.conf.d/90-default.conf
echo 'allow-guest=false'        >> usr/share/lightdm/lightdm.conf.d/60-lightdm-gtk-greeter.conf
cp -f   ../tools/bin/*  usr/local/bin
chroot . chmod +755 -R /usr/local/bin
chroot . chmod +644 -R /boot
chroot . chmod +755 -R /root
chroot . xsession-config

############################################

# zh_CN language packs and timezone
DEBIAN_FRONTEND=noninteractive chroot . apt-get install --no-install-recommends language-pack-zh-hans language-pack-zh-hans-base language-pack-gnome-zh-hans language-pack-gnome-zh-hans-base --yes
chroot . cp -rf usr/share/locale-langpack/zh_CN/LC_MESSAGES/* usr/share/locale/zh_CN/LC_MESSAGES/
chroot . rm -rf usr/share/locale-langpack/
chroot . rm -f etc/timezone
chroot . rm -f etc/localtime
chroot . rm -f etc/adjtime
chroot . ln -s /usr/share/zoneinfo/Asia/Shanghai etc/localtime
echo '0.0 0 0' >  etc/adjtime
echo '0'       >> etc/adjtime
echo 'LOCAL'   >> etc/adjtime
echo 'Asia/Shanghai'             >  etc/timezone
echo 'export TZ=Asia/Shanghai'   >> etc/profile
echo 'export LANG=zh_CN.UTF-8'   >> etc/profile
echo 'export LC_ALL=zh_CN.UTF-8' >> etc/profile
# user settings
mkdir -p home/ubuntu/Desktop
mkdir -p home/ubuntu/.config
cp -rf ../tools/ubuntu/config/* home/ubuntu/.config/
chmod  +666 -R home/ubuntu/.config/
chmod  +666 -R home/ubuntu/Desktop/
chroot . rm -f home/ubuntu/.config/user-dirs.*
# fcitx-googlepinyin
echo -e "install fcitx-googlepinyin and fonts  " 
DEBIAN_FRONTEND=noninteractive chroot . apt-get install --no-install-recommends ttf-ubuntu-font-family fonts-wqy-microhei fcitx fcitx-ui-classic fcitx-frontend-gtk2 fcitx-config-gtk fcitx-googlepinyin --yes
# fcitx config
echo 'export GTK_IM_MODULE="xim"'    >  etc/X11/Xsession.d/96fcitx_start
echo 'export XMODIFIERS="@im=fcitx"' >> etc/X11/Xsession.d/96fcitx_start
cp -f usr/local/bin/97pinyin*      etc/X11/Xsession.d/
cp -f usr/local/bin/fcitx-pinyin*  usr/bin
cp ../tools/settings/*.desktop               usr/share/applications
cp ../tools/settings/fcitx-config.desktop    etc/xdg/autostart/
# mate desktop environment
echo -e " mate desktop environment " 
DEBIAN_FRONTEND=noninteractive chroot . apt-get install --no-install-recommends mate-desktop-environment-core lightdm pluma engrampa eom mate-calc --yes
# cd usr/share/themes/ && rm -rf `ls | grep -v Default Adwaita Black* Blue* Green* Menta* ` && cd ../../..
cd usr/share/themes/ && rm -rf ContrastHigh* Clearlook* Crux* eOS* HighContrast* Industria* Mist* Raleigh* Redmond* Traditional*  &&  cd ../../..
# add gonme applications Links
chroot . rm -f /usr/bin/gedit
chroot . rm -f /usr/bin/nautilus
chroot . rm -f /usr/bin/gnome-terminal
chroot . rm -f /usr/bin/file-roller
chroot . rm -f /usr/bin/eog
ln -s pluma usr/bin/gedit
ln -s caja  usr/bin/nautilus
ln -s eom   usr/bin/eog
ln -s engrampa      usr/bin/file-roller
ln -s mate-terminal usr/bin/gnome-terminal
#  modify some desktop
cd usr/share/applications
sed -i 's/GenericName\[zh_CN\]/Name\[zh_CN\]/' engrampa.desktop
sed -i 's/GenericName\[zh_CN\]/Name\[zh_CN\]/' mate-terminal.desktop
sed -i 's/GenericName\[zh_CN\]/Name\[zh_CN\]/' caja-browser.desktop
sed -i 's/Exec=fcitx/Exec=im-config/' fcitx.desktop
sed -i 's/Settings/System/' fcitx-con*.desktop
sed -i 's/NoDisplay\=true//' python*.desktop
sed -i 's/System\;//'  mate-terminal.desktop
sed -i 's/System\;//'  caja-browser.desktop
sed -i 's/MATE\ 之眼//' eom.desktop
cd ../../../
# 重新配置网络 and # clean up
chroot . rm -f etc/resolv.conf
chroot . dpkg -i network-manager*.deb
chroot . apt clean
chroot . apt-clean
chroot . apt-get clean
chroot . rm -rf tmp/*
chroot . rm -f network-manager*.deb
chroot . rm -f vmlinu*  initrd.im*
chroot . rm -f etc/init/tty3.conf etc/init/tty4.conf etc/init/tty5.conf
chroot . rm -f /var/lib/dbus/machine-id
chroot . rm -f /sbin/initctl
chroot . dpkg-divert --rename --remove /sbin/initctl
umount -lf sys/
umount -lf proc/
umount -lf dev/pts/
umount -lf dev/
cd ..

############################################

# ubuntu live base system is Success
  echo -e "ubuntu live base system is successful, you can diy it in squashfs-root contents "   
  echo -e ". Do you want to make an ISO image ? [y/n] "           
  read choice
  if [ "$choice" != "y" ]; then
      echo -e "you can run ' sudo bash mkiso.sh ' to build an iso LiveCD;successful "           
      exit 0
  fi

# make iso file ; build an iso image
mkdir -p cdimage/casper
rm -f cdimage/casper/*
rm -f $IMAGE.iso
cp -f squashfs-root/boot/vmlinuz-*-generic cdimage/casper/vmlinuz
cp -f squashfs-root/boot/initrd.img-*-generic cdimage/casper/initrd.lz
mksquashfs squashfs-root cdimage/casper/filesystem.squashfs -e boot
chmod +666 -R cdimage/casper
mkdir -p cdimage/.disk
touch cdimage/ubuntu
touch cdimage/.disk/base_installable
echo "full_cd/single"        > cdimage/.disk/cd_type
echo "Ubuntu Remix $RELEASE" > cdimage/.disk/info
cd cdimage && find . -type f -print0 | xargs -0 md5sum | grep -v "\./md5sum.txt" > md5sum.txt && cd ..
mkisofs -o $IMAGE.iso -b isolinux/isolinux.bin -c isolinux/boot.cat \
     -no-emul-boot -boot-load-size 4 -boot-info-table -r -V "$IMAGE" -cache-inodes -J -l cdimage/
chmod  +666 $IMAGE.iso
cd cdimage && rm -f ubuntu md5sum.txt  && cd ..
rm -rf cdimage/.disk
rm -rf cdimage/casper/*
echo -e "successfully done"


